package com.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.reggie.dto.SetmealDto;
import com.reggie.exception.CustomException;
import com.reggie.mapper.SetmealMapper;
import com.reggie.pojo.Dish;
import com.reggie.pojo.Setmeal;
import com.reggie.pojo.SetmealDish;
import com.reggie.service.DishService;
import com.reggie.service.SetmealDishService;
import com.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service("setmealService")
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private DishService dishService;

    /**
     * 新增套餐，同时保存套餐与菜品的关系
     * @param setmealDto
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveWithDish(SetmealDto setmealDto) {
        //保存套餐基本信息
        this.save(setmealDto);

        saveSetmealDish(setmealDto);
    }

    /**
     * 删除套餐，同时删除菜品关联信息
     * @param ids
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteWithDish(List<Long> ids) {
        //查看套餐状态，确定是否可以删除
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(Setmeal::getId, ids);
        wrapper.eq(Setmeal::getStatus, 1);

        int count = this.count(wrapper);
        if (count > 0) {
            //存在在售商品，抛出业务异常
            throw new CustomException("套餐正在售卖中，不可删除！");
        }
        //未发现在售套餐，先删除套餐信息
        this.removeByIds(ids);
        //删除套餐与菜品关联信息
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(SetmealDish::getSetmealId, ids);

        setmealDishService.remove(queryWrapper);
    }

    /**
     * 查询套餐信息及套餐菜品信息
     * @param id
     * @return
     */
    @Override
    public SetmealDto getByIdWithDish(Long id) {
        //获取套餐基本信息
        Setmeal setmeal = this.getById(id);
        //创建一个返回对象
        SetmealDto setmealDto = new SetmealDto();
        //基本信息拷贝
        BeanUtils.copyProperties(setmeal, setmealDto);

        //获取菜品信息
        //首先根据关系表查出关联菜品的id
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId, id);
        List<SetmealDish> dishList = setmealDishService.list(wrapper);

        //根据id例表获取菜品信息
        /*LambdaQueryWrapper<Dish> dishWrapper = new LambdaQueryWrapper<>();
        dishWrapper.in(Dish::getId, dishList);
        List<Dish> list = dishService.list(dishWrapper);*/

        //将菜品信息赋值
        setmealDto.setSetmealDishes(dishList);
        return setmealDto;
    }

    /**
     * 更新套餐与菜品
     * @param setmealDto
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateWithDish(SetmealDto setmealDto) {
        //更新基本信息
        this.updateById(setmealDto);
        //清除原先的菜品与套餐的关联关系
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId, setmealDto.getId());
        setmealDishService.remove(wrapper);

        saveSetmealDish(setmealDto);
    }

    /**
     * 保存套餐与菜品关系
     * @param setmealDto
     */
    private void saveSetmealDish(SetmealDto setmealDto) {
        //添加新的菜品与套餐的关系
        List<SetmealDish> setmealDishList = setmealDto.getSetmealDishes();
        setmealDishList.stream().peek((item) -> {
            item.setSetmealId(setmealDto.getId());
        }).collect(Collectors.toList());

        //保存套餐和菜品的关联信息，操作setmeal_dish
        setmealDishService.saveBatch(setmealDishList);
    }
}
